package com.gacu.viewer.client.ui.menu;

import com.gacu.viewer.client.Rna_ss_viewer;
import com.gacu.viewer.client.rna.RNAssDrawable;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.CustomButton;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ImageBundle;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimpleCheckBox;

public class Strand extends HorizontalPanel {

    private RNAssDrawable        poolObj    = null;
    private final SimpleCheckBox cbxDisplay = new SimpleCheckBox();
    private final Label          sName      = new Label();

    public interface StrandImageBundle extends ImageBundle {
        @Resource("details.png")
        public AbstractImagePrototype detailsButtonIcon();

        @Resource("red_x.png")
        public AbstractImagePrototype cancelButtonIcon();
    }

    StrandImageBundle imagesBundle = GWT.create(StrandImageBundle.class);

    public Strand(RNAssDrawable o) {
        super();
        poolObj = o;

        // Setup main panel
        DOM.setStyleAttribute(getElement(), "border", "1px solid #e3e8f3");
        setSize("90%", "20px");
        setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);

        // Create and setup checkbox
        cbxDisplay.setChecked(true);
        cbxDisplay.setPixelSize(16, 16);
        ClickHandler cbxClickHandler = new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                poolObj.setVisible(cbxDisplay.isChecked());
            }
        };
        cbxDisplay.addClickHandler(cbxClickHandler);
        add(cbxDisplay);

        // Create and setup label for the name of the strand
        sName.setPixelSize(132, 20);
        updateName();
        add(sName);

        // Create and setup Edit button
        Image iE = imagesBundle.detailsButtonIcon().createImage();
        CustomButton btnEdit = new CustomButton(iE) {
            @Override
            protected void onClick() {
                Rna_ss_viewer.propMenu.show(poolObj);
            }
        };
        btnEdit.setPixelSize(16, 16);
        btnEdit.setTitle("Details");
        add(btnEdit);

        // TODO Include the image in the project
        Image iD = imagesBundle.cancelButtonIcon().createImage();
        CustomButton btnDel = new CustomButton(iD) {
            @Override
            protected void onClick() {
                if (DOM.eventGetCtrlKey(DOM.eventGetCurrentEvent()) || Window.confirm("Are you sure you want to delete this strand ?")) {
                    Rna_ss_viewer.rnaPool.removeFromPool(poolObj);
                }
            }
        };
        btnDel.setSize("16px", "16px");
        btnDel.setTitle("Press Ctrl to delete without confirmation.");
        add(btnDel);

        // Setup Widget
        MouseOverHandler overHandler = new MouseOverHandler() {
            @Override
            public void onMouseOver(MouseOverEvent event) {
                DOM.setStyleAttribute(getElement(), "backgroundColor", "#e3e8f3");
                DOM.setStyleAttribute(getElement(), "border", "1px solid lightgrey");
            }
        };
        addDomHandler(overHandler, MouseOverEvent.getType());
        MouseOutHandler leaveHandler = new MouseOutHandler() {

            @Override
            public void onMouseOut(MouseOutEvent event) {
                DOM.setStyleAttribute(getElement(), "backgroundColor", "white");
                DOM.setStyleAttribute(getElement(), "border", "1px solid #e3e8f3");
            }
        };
        addDomHandler(leaveHandler, MouseOutEvent.getType());
    }

    public String getName() {
        return sName.getText();
    }

    public void updateName() {
        int maxWidth = 14;
        sName.setTitle(poolObj.getName());
        if (poolObj.getName().length() > maxWidth) {
            sName.setText(poolObj.getName().substring(0, maxWidth - 1) + "...");
        } else {
            sName.setText(poolObj.getName());
        }
    }
}
